import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 14-4-27.
 */
public class WordDomainCheck2 {
    public static void main(String[] args) throws Exception {
        String read_file="高中英语单词表2.txt";
        String write_file="未注册域名.txt";
        String base_url="http://pandavip.www.net.cn/check/check_ac1.cgi?domain=";
        doProcess(read_file,write_file,base_url);
        System.out.println("over");
    }

    public static void doProcess(String read_file,String write_file,String base_url) throws Exception {
        String s=null;
        String word =null;
        String url=null;
        String json =null;
        List<String> file_content =getFileContent(read_file);
        for(String str:file_content){
            word= str.split(" ")[0];
            url=base_url+word+".com";
            json=getJsonString(url);
            if(json.contains("210")){
                System.out.println(url+"------"+json);
                FileAppend.method1(write_file, url + "-----" + json + '\n');
            }else{
                System.out.println(url+"------false");

            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }



    public static List<String> getFileContent(String file_name) throws IOException {
        FileInputStream fis = new FileInputStream(file_name);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String s =null;
        List<String> content = new ArrayList<String>();
        while((s=br.readLine())!=null){
            content.add(s);
        }
        br.close();
        return content;
    }

    public static String getJsonString(String urlPath) throws Exception {
        URL url = new URL(urlPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        //对应的字符编码转换
        Reader reader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String str = null;
        StringBuffer sb = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null) {
            sb.append(str);
        }
        reader.close();
        connection.disconnect();
        return sb.toString();
    }
}
